<?php


namespace App\Admin\Controllers;


use App\Models\Setting;
use Illuminate\Http\Request;
use SmallRuralDog\Admin\Layout\Content;
use SmallRuralDog\Admin\Controllers\AdminController;
use SmallRuralDog\Admin\Controllers\AdminResource;
use SmallRuralDog\Admin\Form;
use SmallRuralDog\Admin\Grid;
use SmallRuralDog\Admin\Components\Form\RadioGroup;
use SmallRuralDog\Admin\Components\Form\Radio;
use SmallRuralDog\Admin\Components\Widgets\Divider;

class SettingController extends AdminController
{
    public function index(Content $content)
    {
        return $content->body($this->form());
    }

    public function form($isEdit = false)
    {
        $form = new Form(new Setting());

        $form->action(route('admin.setting.set'));

        $form->labelWidth("250px");

        // $setting = $form->model()->where('code', 'payment_sumpay')->first();
        //  // dd($setting, $setting->value);
        // if (isset($setting->value))
        // {
        //     $payment_sumpay = json_decode($setting->value, 1);
        // }

        // if (!$payment_sumpay)
        // {
        //     $payment_sumpay = [
        //         'open' => '0',
        //         'app_id' => '',
        //     ];
        // }

        $setting = $form->model()->where('code', 'payment_lianlianpay')->first();
         // dd($setting, $setting->value);
        if (isset($setting->value))
        {
            $payment_lianlianpay = json_decode($setting->value, 1);
        }

        if (!isset($payment_lianlianpay))
        {
            $payment_lianlianpay = [
                'open' => '0',
                // 'app_id' => '',
            ];
        }

        // $form->item('score_return', "积分返还")->component(RadioGroup::make($score_return)->options([
        //     Radio::make('1', "返还"),
        //     Radio::make('0', "不返还"),
        // ]))->topComponent(Divider::make("订单设置"))->help("当订单作废时是否返还积分");

        // $form->item('payment_sumpay_open', "支付开关")->component(RadioGroup::make($payment_sumpay['open'])->options([
        //     Radio::make('1', "开启"),
        //     Radio::make('0', "关闭"),
        // ]))->topComponent(Divider::make("统统付支付设置"))->help("统统付支付配置");

        $form->item('payment_lianlianpay_open', "支付开关")->component(RadioGroup::make($payment_lianlianpay['open'])->options([
            Radio::make('1', "开启"),
            Radio::make('0', "关闭"),
        ]))->topComponent(Divider::make("连连付支付设置"))->help("连连付支付设置");


        // $form->item('payment_sumpay_app_id', '商户号')->defaultValue($payment_sumpay['app_id']);

        return $form;
    }

    public function saveSettings(Request $request)
    {
        try {

            // $score_return = $request->input("score_return");

            // Setting::query()->where('code', 'score_return')->updateOrCreate([],
            //     [
            //         'code' => 'score_return',
            //         'name' => '作废订单是否返还积分',
            //         'value' => $score_return,
            //     ]
            // );

            // $payment_sumpay_open = $request->input("payment_sumpay_open");
            // $payment_sumpay_app_id = $request->input("payment_sumpay_app_id");

            // $payment_sumpay = [];
            // if (isset($payment_sumpay_open) && isset($payment_sumpay_app_id))
            // {
            //     $payment_sumpay = [
            //         'open' => $payment_sumpay_open,
            //         'app_id' => $payment_sumpay_app_id
            //     ];
            // }
            // else
            // {
            //     $payment_sumpay = [
            //         'open' => '0',
            //         'app_id' => '',
            //     ];
            // }
            // // dd($payment_sumpay);
            // Setting::query()->where('code', 'payment_sumpay')->updateOrCreate([],
            //     [
            //         'code' => 'payment_sumpay',
            //         'name' => '统统付支付设置',
            //         'value' => json_encode($payment_sumpay),
            //     ]
            // );

            $payment_lianlianpay_open = $request->input("payment_lianlianpay_open");
            // $payment_sumpay_app_id = $request->input("payment_sumpay_app_id");

            $payment_lianlianpay = [];
            if (isset($payment_lianlianpay_open))
            {
                $payment_lianlianpay = [
                    'open' => $payment_lianlianpay_open,
                    // 'app_id' => $payment_sumpay_app_id
                ];
            }
            else
            {
                $payment_lianlianpay = [
                    'open' => '0',
                    // 'app_id' => '',
                ];
            }
            // dd($payment_sumpay);
            Setting::query()->where('code', 'payment_lianlianpay')->updateOrCreate([],
                [
                    'code' => 'payment_lianlianpay',
                    'name' => '连连付支付设置',
                    'value' => json_encode($payment_lianlianpay),
                ]
            );

            return \Admin::responseRedirect(url('admin#/setting'),false, '保存成功');

        } catch (\Exception $exception) {
            return \Admin::responseError($exception->getMessage());
        }
    }


    public function uploadImage(Request $request)
    {
        try {

            \Admin::validatorData($request->all(), [
                'file[]' => 'image'
            ]);

            $disk = config('admin.upload.disk');
            foreach ($request->file('file') as $file)
            {
                $name = $file->getClientOriginalName();
                $path = $file->storeAs('images', $name, $disk);
                $url[] = \Storage::disk($disk)->url($path);;
            }

            $data = ['errno' => 0, 'data' => $url];

            return $data;
        } catch (\Exception $exception) {

            return \Admin::responseError($exception->getMessage());

        }
    }

}
